import os, sys, re
from invenio.config import CFG_WEBSUBMIT_BIBCONVERTCONFIGDIR as configdir
from invenio.websubmit_config import InvenioWebSubmitFunctionError

def Make_Record_Python(parameters, curdir, form, user_info=None):
	data = {}
	for f in os.listdir(curdir):
		if os.path.isfile(os.path.join(curdir, f)):
			datafile = open(os.path.join(curdir, f), "r")
			content = datafile.read().strip()
			content = re.sub("[\n\r]+", "\n", content)
			data[f] = content
			datafile.close()
			
	if configdir not in sys.path:
		sys.path.append(configdir)
	try:
		makerec = __import__(os.path.splitext(parameters["createTemplate"])[0]).convert
	except:
		raise InvenioWebSubmitFunctionError("Error occured wneh getting stransformation script")
	recfile = file(os.path.join(curdir, "recmysql"), "w")
	try:
		recfile.write(makerec(data))
	except:
		raise InvenioWebSubmitFunctionError("Cannot create database record")
	recfile.close()
    
	return ""
    
if __name__ == "__main__":
	Make_Record_Python({"createTemplate" : "create_database_record.py"}, "/root/websubmittest/nusl-27774-000001_20110902140446/", "")
